Skip to main content

cmp command

cmp - compare two files byte by byte

The cmp command in Linux is a utility used to compare two files byte-by-byte. Unlike diff, which shows line-by-line differences in text files, cmp is more low-level and works with any file type (text or binary). It’s useful for quickly checking if files are identical or pinpointing where they diverge.

Usage: cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]

  • OPTION: Flags which enhances the diff abilities.
  • FILE1: The file to compare.
  • FILE2: The file to compare.
  • SKIP1: Optional byte offsets specifying the number of bytes to skip
  • SKIP2: Optional byte offsets specifying the number of bytes to skip

Examples

  • Basic Comparison

    Run cmp with two files to check if they differ.

    $ cmp file1.txt file2.txt
    • Sample files:
      • file1.txt: hello world
      • file2.txt: hello there
    • Output: file1.txt file2.txt differ: byte 7, line 1
    • Explanation:
      • Files differ at byte 7 (where "world" and "there" diverge).
      • If files are identical, cmp produces no output and exits silently.
  • Silent Mode

    Use -s (silent) to suppress output and only check the exit status.

    $ cmp -s file1.txt file2.txt
    • No output, but:
      • Exit status 0 = files are identical.
      • Exit status 1 = files differ.
      • Check with: echo $? after running.
  • Verbose Output

    Use -l (long) to list all differing bytes.

    $ cmp -l file1.txt file2.txt
    • Output:
      7  167  164
      8 157 150
      9 162 145
      10 154 162
      11 144 145
    • Explanation:
      • Byte 7: w (octal 167) vs. t (octal 164).
      • Byte 8: o (157) vs. h (150), etc.
      • Shows every mismatch in octal values.
  • Limiting Output

    Use -n to compare only a specific number of bytes.

    $ cmp -n 5 file1.txt file2.txt
    • Compares only the first 5 bytes (hello in both files).
    • Output: (none, since they match up to byte 5).
  • Skipping Bytes

    Specify byte offsets (skip1 and skip2) to start comparison past the beginning.

    $ cmp file1.txt file2.txt 6
    • Skips 6 bytes in file1.txt (starts at "world") and compares with the start of file2.txt.

    • Output depends on the remaining content.

    • Common Options

    OptionDescription
    -sSilent mode (no output, just exit status)
    -lList all differing bytes (verbose)
    -nLimit comparison to N bytes
    -bPrint differing bytes as characters
  • Printing Differing Bytes as Characters

    Use -b to show byte differences as printable characters.

    $ cmp -b file1.txt file2.txt
    • Output: file1.txt file2.txt differ: byte 7, line 1 is 167 w 164 t
    • Shows w vs. t instead of just octal values.
$ cmp --help
Usage: cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
Compare two files byte by byte.

The optional SKIP1 and SKIP2 specify the number of bytes to skip
at the beginning of each file (zero by default).

Mandatory arguments to long options are mandatory for short options too.
-b, --print-bytes print differing bytes
-i, --ignore-initial=SKIP skip first SKIP bytes of both inputs
-i, --ignore-initial=SKIP1:SKIP2 skip first SKIP1 bytes of FILE1 and
first SKIP2 bytes of FILE2
-l, --verbose output byte numbers and differing byte values
-n, --bytes=LIMIT compare at most LIMIT bytes
-s, --quiet, --silent suppress all normal output
--help display this help and exit
-v, --version output version information and exit

SKIP values may be followed by the following multiplicative suffixes:
kB 1000, K 1024, MB 1,000,000, M 1,048,576,
GB 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y.

If a FILE is '-' or missing, read standard input.
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.